home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 775 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.9 KB

  1. From: clamage@Eng.Sun.COM (Steve Clamage)
  2. Message-ID: <4ikmol$b5u@engnews1.Eng.Sun.COM>
  3. X-Original-Date: 18 Mar 1996 22:06:13 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 19 Mar 96 01:16:50 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: FOR comp.std.c++: problems with I/O except
  9. Organization: Sun Microsystems Inc.
  10. References: <199603181627.RAA08990@bredex.bredex.de>
  11. Reply-To: clamage@Eng.Sun.COM
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMU49BOEDnX0m9pzZAQE+dgF/QndLdHSVOgBUQjNOvpuAIvg9s5G5CNF7
  14.     cumOhtISUsm+F9Qv9dcB2iZhHTcxvwMZ
  15.     =7DDD
  16.  
  17. In article RAA08990@bredex.bredex.de, Nico Josuttis <nico@bredex.de> writes:
  18. >Hi,
  19. >Trying the new feature of throwing stream exceptions
  20. >i wrote the following program:
  21.  
  22.      [ trimmmed ]
  23.  
  24. >    try {
  25. >        int value, sum;
  26. >
  27. >        /* while not EOF
  28. >         * read and add value
  29. >         */
  30. >        sum = 0;
  31. >        while (cin >> value) {
  32. >            sum += value;
  33. >        }
  34. >
  35. >        cout << "sum: " << sum << endl;
  36. >
  37. >    }
  38. >    catch (ios::failure error) {
  39. >        cerr << "Error: " << error.what() << endl;
  40. >        exit (EXIT_FAILURE);
  41. >    }
  42.  
  43. >But it didn't work as I expected, because also on EOF
  44. >I ALWAYS got the failbit exception!
  45.  
  46. >The problem is, that istream::ipfx() always calls setstate(failbit),
  47. >if after any preparation good() is false.
  48. >So even if only the eofbit is set (due to the skip of whitespace),
  49. >the failbit is also set.
  50.  
  51. That is the desired behavior, and your program should take it into account.
  52.  
  53. For example, suppose the file ends with whitespace (which it often will,
  54. if only because of a final newline before the EOF). You attempt to read
  55. an int value, whitespace is skipped, and EOF is reached before any
  56. valid characters are found. That is a failure, by definition. The failure
  57. cannot be predicted before starting the input, since characters are
  58. available at the start. As it happens, you are not interested in any
  59. of the characters which are available.
  60.  
  61. Now suppose that EOF is already true when starting the input. Again by
  62. definition, input fails, since no characters are available for reading.
  63.  
  64. The ipfx() function is called as part of starting an input operation.
  65. You are asking for input, and to be notified if the input fails. It fails
  66. -- no input available -- and you are notified.
  67.  
  68. When you have a loop like
  69.     while( cin >> value ) { ... }
  70. it will ALWAYS end by failing, since the loop only ends by finding improper
  71. input or reading past EOF, and reading past EOF is a failure. Thus,
  72. you probably do not want to use an exception to detect the end of the
  73. loop.
  74.  
  75. > ... In general I need a chance to handle eof without getting
  76. >failbit exceptions.
  77.  
  78. I think it is more likely that you don't want to throw an exception on
  79. failure unless failure is not expected to happen. When reading input up
  80. to the end, failure will always occur. A schema like the following
  81. gives you the control you want without using exceptions:
  82.  
  83.     while( cin >> value ) {
  84.         ... do something with value
  85.     }
  86.     if( cin.eof() ) ... no more input available
  87.     else ... error in input
  88.  
  89. More likely, you would write a single test like this:
  90.     if( ! cin.eof() ) ... error in input
  91.  
  92. Exceptions are best used for those cases where you don't expect
  93. them to be triggered, and you need to escape to a higher level in the
  94. program when they do occur. Exceptions give you a way to escape without
  95. having to pass error status explicitly through a chain of function calls.
  96. Don't use exceptions for normal loop control; they are inefficient for
  97. that purpose and cause you to write more complicated code instead of
  98. less complicated code.
  99.  
  100. ---
  101. Steve Clamage, stephen.clamage@eng.sun.com
  102. ---
  103. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  104. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  105. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  106. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  107. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  108.